package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.annotation.UserRecordTimes;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.controller.request.SecondBatchCancellationOrderRequest;
import com.sc.nft.dao.*;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.DigitalMarketTypeEnum;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.exception.ModelException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.NewSecondCollectionCommonService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.compress.utils.Lists;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/newSecondCollection")
@Api(tags = "新的2级模块")
public class NewSecondCollectionCommonController {

    private final NewSecondCollectionCommonService newSecondCollectionCommonService;

    private final RedissonClient redissonClient;
    private final RedisTemplate redisTemplate;
    private final FilterIncomeListDao filterIncomeListDao;
    private final UserCollectionDao userCollectionDao;
    private final SecondCollectionDao secondCollectionDao;
    private final DigitalCollectionDao digitalCollectionDao;
    private final ExitConsensusUserDao exitConsensusUserDao;


    @PostMapping(value = "/collectionBuyConfirm", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取藏品购买确认页面数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<CollectionBuyConfirmVO> collectionBuyConfirm(@RequestBody CollectionBuyConfirmDTO newSecondCollectionLockOrder) {
        return Result.success(newSecondCollectionCommonService.collectionBuyConfirm(UserPool.getUser(), newSecondCollectionLockOrder));
    }

    @PostMapping(value = "/selectGoodsTotalPrice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取选择商品的总价", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result selectGoodsTotalPrice(@RequestBody GoodsTotalMoneyDTO goodsTotalMoneyDTO) {
        BigDecimal bigDecimal = newSecondCollectionCommonService.selectGoodsTotalPrice(goodsTotalMoneyDTO);
        Map map = new HashedMap();
        map.put("price", bigDecimal.stripTrailingZeros().toPlainString());
        return Result.success(map);
    }

    @PostMapping(value = "/orderNotPayPrice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "未支付订单价格", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result orderNotPayPrice(@RequestBody OrderNotPayPriceDTO orderNotPayPriceDTO) {
        BigDecimal bigDecimal = newSecondCollectionCommonService.orderNotPayPrice(orderNotPayPriceDTO);
        Map map = new HashedMap();
        map.put("price", bigDecimal.stripTrailingZeros().toPlainString());
        return Result.success(map);
    }

    @PostMapping(value = "/lockOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "锁单(生成2级订单,返回订单号)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    @UserRecordTimes
    public Result<LockOrderVO> lockOrder(@RequestBody @Validated NewSecondCollectionLockOrder newSecondCollectionLockOrder) {
        String lockKey = RedisKeyEnum.SECOND_USER_LOCK.getKey() + UserPool.getUser().getId();
        LockOrderVO lockOrderVO;
        FilterIncomeList filterIncomeList = filterIncomeListDao.getByUserId(UserPool.getUser().getId());
        if (ObjectUtil.isNull(filterIncomeList) && BooleanUtil.isFalse(UserPool.getUser().getIsSm())) {
            return Result.fail(ErrorCode.PLEASE_PERFORM_REAL_NAME_AUTHENTICATION_FIRST);
        }else{
            Boolean b = redisTemplate.hasKey(lockKey);
            if (b) {
                Integer lockSum = Convert.toInt(redisTemplate.opsForValue().get(lockKey));
                if (lockSum >= 3) {
                    throw new GlobalRunTimeException(ErrorCode.ARE_MORE_THAN_3_UNPAID_ORDERS_ON_THE_DAY_WHICH_WILL_BE_AUTOMATICALLY_CANCELLED);
                }
            }
        }

        List<RLock> lock = new ArrayList();
        try {
            // 用来存放被锁的id
            List<Long> mallLockId = new ArrayList<>();
            newSecondCollectionLockOrder.getSecondCollectionIds().forEach(aLong -> {
                // lock的key组成  LOCK_ORDER挂售id
                String key = StrUtil.format("LOCK_ORDER:{}", aLong);
                RLock orderLock = redissonClient.getLock(key);
                log.info("二级锁单Key:{}", orderLock);
                boolean heldByCurrentThread = orderLock.tryLock();
                if (!heldByCurrentThread) {
                    // 把挂载的商品id返回去.
                    mallLockId.add(aLong);
                } else {
                    lock.add(orderLock);
                }
            });
            if (CollUtil.isNotEmpty(mallLockId)) {
                throw new ModelException(mallLockId, ErrorCode.COUNT_NOT_ENOUGH);
            }
            log.info("锁单入参请求参数:{}", JSON.toJSON(newSecondCollectionLockOrder));



            lockOrderVO = newSecondCollectionCommonService.lockOrder(UserPool.getUser(), newSecondCollectionLockOrder);
        } finally {
            // 批量释放锁
            lock.forEach(rLock -> {
                rLock.unlock();
                log.info("二级释放Key:{}", rLock);

            });
        }
        redisTemplate.opsForValue().increment(lockKey);
        redisTemplate.expire(lockKey, 1, TimeUnit.HOURS);
        return Result.success(lockOrderVO);
    }


    @PostMapping(value = "/lockBrandOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "孵化市场锁单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    @UserRecordTimes
    @SneakyThrows
    public Result<LockOrderVO> lockBrandOrder(@RequestBody @Validated SecondBrandCollectionLockOrderDTO request) {
        String lockKey = RedisKeyEnum.SECOND_USER_LOCK.getKey() + UserPool.getUser().getId();
        FilterIncomeList filterIncomeList = filterIncomeListDao.getByUserId(UserPool.getUser().getId());
        if (ObjectUtil.isNull(filterIncomeList) && BooleanUtil.isFalse(UserPool.getUser().getIsSm())) {
            throw new GlobalRunTimeException(ErrorCode.PLEASE_PERFORM_REAL_NAME_AUTHENTICATION_FIRST);
        }else{
            Boolean b = redisTemplate.hasKey(lockKey);
            if (b) {
                Integer lockSum = Convert.toInt(redisTemplate.opsForValue().get(lockKey));
                if (lockSum >= 3) {
                    throw new GlobalRunTimeException(ErrorCode.ARE_MORE_THAN_3_UNPAID_ORDERS_ON_THE_DAY_WHICH_WILL_BE_AUTOMATICALLY_CANCELLED);
                }
            }
        }
        LockOrderVO lockOrderVO = newSecondCollectionCommonService.lockBrandOrder(UserPool.getUser(),request);
        redisTemplate.opsForValue().increment(lockKey);
        redisTemplate.expire(lockKey, 1, TimeUnit.HOURS);
        return Result.success(lockOrderVO);

    }





    @PostMapping(value = "/newSecondCollectionPay", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新2级订单支付", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result newSecondCollectionPay(@RequestBody @Validated NewSecondCollectionPayDTO newSecondCollectionPayDTO) {
        return Result.fail("请下载最新版本");
//        return Result.success(newSecondCollectionCommonService.newSecondCollectionPay(UserPool.getUser(), newSecondCollectionPayDTO));
    }

    @PostMapping(value = "/newSecondCollectionGoodsBuyList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新二级商品批量购买的时候的购买列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<NewSecondCollectionGoodsBuyListVO>> newSecondCollectionGoodsBuyList(@RequestBody @Validated NewSecondCollectionGoodsBuyListDTO newSecondCollectionGoodsBuyListDTO) {
        return Result.success(newSecondCollectionCommonService.newSecondCollectionGoodsBuyList(UserPool.getUser(), newSecondCollectionGoodsBuyListDTO));
    }

    @PostMapping(value = "/selectNewSecondCollectionGoodsSaleOrderList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户的卖出订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<Page<NewSecondCollectionGoodsBuyOrderListVO>> selectNewSecondCollectionGoodsSaleOrderList(@RequestBody NewSecondCollectionGoodsBuyOrderListDTO newSecondCollectionGoodsBuyOrderListDTO) {
        return Result.success(newSecondCollectionCommonService.selectNewSecondCollectionGoodsSaleOrderList(newSecondCollectionGoodsBuyOrderListDTO));
    }

    @PostMapping(value = "/newSecondCollectionGoodsBuyOrderList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户的买入订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<Page<NewSecondCollectionGoodsBuyOrderListVO>> newSecondCollectionGoodsBuyOrderList(@RequestBody NewSecondCollectionGoodsBuyOrderListDTO newSecondCollectionGoodsBuyOrderListDTO) {
        return Result.success(newSecondCollectionCommonService.selectNewSecondCollectionGoodsBuyOrderList(newSecondCollectionGoodsBuyOrderListDTO));
    }


    @PostMapping(value = "/overdueOrderList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "取消的订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<NewSecondCollectionGoodsBuyOrderListVO> overdueOrderList(@Validated @RequestBody PageRequest request) {
        return PageResult.success(newSecondCollectionCommonService.overdueOrderList(UserPool.getUser(),request.toMp()));
    }

    @PostMapping(value = "/overdueOrderDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "取消订单详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SecondOverdueOrderDetailsVO> overdueOrderDetails(@RequestBody @Validated IdRequest idRequest) {
        return Result.success(newSecondCollectionCommonService.overdueOrderDetails(idRequest.getId(), UserPool.getUser().getId()));
    }

    @PostMapping(value = "/newSecondCollectionNotPay", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "新二级合并支付待付款订单数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<NewSecondCollectionNotPayVO> newSecondCollectionNotPay() {
        return Result.success(newSecondCollectionCommonService.newSecondCollectionNotPay());
    }

    @PostMapping(value = "/nweSecondCollectionOrderDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据订单号获取这条订单买入的数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<NewSecondCollectionOrderDetailsVO> nweSecondCollectionOrderDetails(@RequestBody @Validated IdRequest idRequest) {
        return Result.success(newSecondCollectionCommonService.nweSecondCollectionOrderDetails(idRequest.getId()));
    }

    @PostMapping(value = "/nweSecondCollectionOrderSaleDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "根据订单号获取这条订单卖出的数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<NewSecondCollectionOrderDetailsVO> nweSecondCollectionOrderSaleDetails(@RequestBody @Validated IdRequest idRequest) {
        return Result.success(newSecondCollectionCommonService.nweSecondCollectionOrderSaleDetails(idRequest.getId()));
    }

    @PostMapping(value = "/getUserItemStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户藏品状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetUserItemStatusVO> getUserItemStatus(@RequestBody GetUserItemStatusDTO getUserItemStatusDTO) {
        return Result.success(newSecondCollectionCommonService.getUserItemStatus(getUserItemStatusDTO));
    }

    @PostMapping(value = "/getUserConsignment", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "用户商品寄售的时候详情信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetUserConsignmentVO> getUserConsignment(@RequestBody GetUserConsignmentDTO getUserConsignmentDTO) {
        return Result.success(newSecondCollectionCommonService.getUserConsignment(getUserConsignmentDTO));
    }

    @PostMapping(value = "/getUserConsignmentCollectionPrice", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户寄售藏品价格", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserConsignmentCollectionPriceVO> getUserConsignmentCollectionPrice(@RequestBody UserConsignmentCollectionPriceDTO userConsignmentCollectionPriceDTO) {
        UserConsignmentCollectionPriceVO userConsignmentCollectionPrice = newSecondCollectionCommonService.getUserConsignmentCollectionPrice(userConsignmentCollectionPriceDTO);
        return Result.success(userConsignmentCollectionPrice);
    }

    @PostMapping(value = "/cancellationConsignment", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "取消寄售", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams(
            @ApiImplicitParam(name = "用户藏品id", value = "id")
    )
    @UserLogin
    public Result cancellationConsignment(@RequestBody @Validated CancellationConsignmentDTO cancellationConsignmentDTO) {
        Boolean is = newSecondCollectionCommonService.cancellationConsignment(cancellationConsignmentDTO);
        Map map = new HashedMap();
        map.put("execute", is);
        return Result.success(map);
    }

    @PostMapping(value = "/freeMarketHitShelf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自由市场上架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result freeMarketHitShelf(@Valid @RequestBody FreeMarketHitShelfDTO freeMarketHitShelfDTO) {

        ExitConsensusUser exitConsensusUser = exitConsensusUserDao.getByUserId(UserPool.getUser().getId());
        if(ObjectUtil.isNotNull(exitConsensusUser)&&exitConsensusUser.getStatus()){

        }else{
            if(UserPool.getUser().getCreateTime().before(DateTime.of("2024-07-01 00:00:00","yyyy-MM-dd HH:mm:ss")))
            {

                UserCollection userCollection = userCollectionDao.getById(freeMarketHitShelfDTO.getUserCollectionId());
                DigitalCollection digitalCollection = digitalCollectionDao.getEcologyCollectionById(userCollection.getCollectionId());
                if (ObjectUtil.isNotNull(digitalCollection) && digitalCollection.getCollectionType() == DigitalMarketTypeEnum.ECOLOGY &&digitalCollection.getId().longValue()!=1057) {
                    Integer onShelfByUserIdAndCollectionIdCount = secondCollectionDao.getOnShelfByUserIdAndCollectionIdCount(userCollection.getUserId(), userCollection.getCollectionId());
                    if (1 - onShelfByUserIdAndCollectionIdCount < 0) {
                        return Result.fail("该藏品单次挂单数量已达上限");
                    }
                    if (1 - onShelfByUserIdAndCollectionIdCount - 1 < 0) {
                        return Result.fail("该藏品剩余挂单数量为" + (1 - onShelfByUserIdAndCollectionIdCount));
                    }
                    if (redisTemplate.hasKey(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString())) {
                        Long expire = redisTemplate.getExpire(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString());
                        return Result.fail(StrUtil.format("距离下次挂单时间剩余{}分{}秒", String.valueOf(expire / 60), String.valueOf(expire % 60)));
                    }
                }
            }
        }
        Boolean is = newSecondCollectionCommonService.freeMarketHitShelf(freeMarketHitShelfDTO.getUserCollectionId(), freeMarketHitShelfDTO.getBuyPrice(), freeMarketHitShelfDTO.getDealPwd());
        Map map = new HashedMap();
        map.put("execute", is);
        return Result.success(map);
    }

    @PostMapping(value = "/daoFreeMarketHitShelf", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "dao自由市场上架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result daoFreeMarketHitShelf(@Valid @RequestBody DaoFreeMarketHitShelfDTO freeMarketHitShelfDTO) {
        ExitConsensusUser exitConsensusUser = exitConsensusUserDao.getByUserId(UserPool.getUser().getId());
        if(ObjectUtil.isNotNull(exitConsensusUser)&&exitConsensusUser.getStatus()){

        }else{
            if(UserPool.getUser().getCreateTime().before(DateTime.of("2024-07-01 00:00:00","yyyy-MM-dd HH:mm:ss")))
            {

                UserCollection userCollection = userCollectionDao.getById(freeMarketHitShelfDTO.getUserCollectionId());
                DigitalCollection digitalCollection = digitalCollectionDao.getEcologyCollectionById(userCollection.getCollectionId());
                if (ObjectUtil.isNotNull(digitalCollection) && digitalCollection.getCollectionType() == DigitalMarketTypeEnum.ECOLOGY&&digitalCollection.getId().longValue()!=1057) {
                    Integer onShelfByUserIdAndCollectionIdCount = secondCollectionDao.getOnShelfByUserIdAndCollectionIdCount(userCollection.getUserId(), userCollection.getCollectionId());
                    if (1 - onShelfByUserIdAndCollectionIdCount < 0) {
                        return Result.fail("该藏品单次挂单数量已达上限");
                    }
                    if (1 - onShelfByUserIdAndCollectionIdCount - 1 < 0) {
                        return Result.fail("该藏品剩余挂单数量为" + (1 - onShelfByUserIdAndCollectionIdCount));
                    }
                    if (redisTemplate.hasKey(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString())) {
                        Long expire = redisTemplate.getExpire(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString());
                        return Result.fail(StrUtil.format("距离下次挂单时间剩余{}分{}秒", String.valueOf(expire / 60), String.valueOf(expire % 60)));
                    }
                }
            }
        }
        Boolean is = newSecondCollectionCommonService.daoFreeMarketHitShelf(freeMarketHitShelfDTO.getUserCollectionId(), freeMarketHitShelfDTO.getBuyPrice(), freeMarketHitShelfDTO.getDealPwd(), freeMarketHitShelfDTO.getDaoId());
        Map map = new HashedMap();
        map.put("execute", is);
        return Result.success(map);
    }

    @PostMapping(value = "/daoFreeMarketHitShelfBatch", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "dao自由市场上架(批量)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result daoFreeMarketHitShelfBacth(@Valid @RequestBody DaoFreeMarketHitShelBatchfDTO freeMarketHitShelfDTO) {
        ExitConsensusUser exitConsensusUser = exitConsensusUserDao.getByUserId(UserPool.getUser().getId());
        if(ObjectUtil.isNotNull(exitConsensusUser)&&exitConsensusUser.getStatus()){

        }else{
            if(UserPool.getUser().getCreateTime().before(DateTime.of("2024-07-01 00:00:00","yyyy-MM-dd HH:mm:ss"))) {

                UserCollection userCollection = userCollectionDao.getById(freeMarketHitShelfDTO.getUserCollectionId().get(0));
            DigitalCollection digitalCollection = digitalCollectionDao.getEcologyCollectionById(userCollection.getCollectionId());
            if(ObjectUtil.isNotNull(digitalCollection)&&digitalCollection.getCollectionType()== DigitalMarketTypeEnum.ECOLOGY&&digitalCollection.getId().longValue()!=1057){
                Integer onShelfByUserIdAndCollectionIdCount = secondCollectionDao.getOnShelfByUserIdAndCollectionIdCount(userCollection.getUserId(), userCollection.getCollectionId());
                if(1-onShelfByUserIdAndCollectionIdCount<0){
                    return Result.fail("该藏品单次挂单数量已达上限");
                }
                if(1-onShelfByUserIdAndCollectionIdCount-freeMarketHitShelfDTO.getUserCollectionId().size()<0){
                    return Result.fail("该藏品剩余挂单数量为"+(1-onShelfByUserIdAndCollectionIdCount));
                }
                if(redisTemplate.hasKey(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey()+UserPool.getUser().getId().toString())){
                    Long expire = redisTemplate.getExpire(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString());
                    return Result.fail(StrUtil.format("距离下次挂单时间剩余{}分{}秒",String.valueOf(expire/60),String.valueOf(expire%60)));
                }
            }
            }
        }
        Boolean is = Boolean.FALSE;
        for (Long l : freeMarketHitShelfDTO.getUserCollectionId()) {
            is = newSecondCollectionCommonService.daoFreeMarketHitShelf(l, freeMarketHitShelfDTO.getBuyPrice(), freeMarketHitShelfDTO.getDealPwd(), freeMarketHitShelfDTO.getDaoId());
        }
        Map map = new HashedMap();
        map.put("execute", is);
        return Result.success(map);
    }

    @PostMapping(value = "/freeMarketHitShelfBatch", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自由市场批量上架", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result freeMarketHitShelfBatch(@Valid @RequestBody FreeMarketHitShelfBatchDTO freeMarketHitShelfBatchDTO) {
        Boolean is = Boolean.FALSE;
        ExitConsensusUser exitConsensusUser = exitConsensusUserDao.getByUserId(UserPool.getUser().getId());
        if(ObjectUtil.isNotNull(exitConsensusUser)&&exitConsensusUser.getStatus()){

        }else{
            if(UserPool.getUser().getCreateTime().before(DateTime.of("2024-07-01 00:00:00","yyyy-MM-dd HH:mm:ss"))) {
                UserCollection userCollection = userCollectionDao.getById(freeMarketHitShelfBatchDTO.getUserCollectionId().get(0));
                DigitalCollection digitalCollection = digitalCollectionDao.getEcologyCollectionById(userCollection.getCollectionId());
                if (ObjectUtil.isNotNull(digitalCollection) && digitalCollection.getCollectionType() == DigitalMarketTypeEnum.ECOLOGY&&digitalCollection.getId().longValue()!=1057) {
                    Integer onShelfByUserIdAndCollectionIdCount = secondCollectionDao.getOnShelfByUserIdAndCollectionIdCount(userCollection.getUserId(), userCollection.getCollectionId());
                    if (1 - onShelfByUserIdAndCollectionIdCount < 0) {
                        return Result.fail("该藏品单次挂单数量已达上限");
                    }
                    if (1 - onShelfByUserIdAndCollectionIdCount - freeMarketHitShelfBatchDTO.getUserCollectionId().size() < 0) {
                        return Result.fail("该藏品剩余挂单数量为" + (1 - onShelfByUserIdAndCollectionIdCount));
                    }
                    if (redisTemplate.hasKey(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString())) {
                        Long expire = redisTemplate.getExpire(RedisKeyEnum.CONSENSUS_CONSIGN_LOCK.getKey() + UserPool.getUser().getId().toString());
                        return Result.fail(StrUtil.format("距离下次挂单时间剩余{}分{}秒", String.valueOf(expire / 60), String.valueOf(expire % 60)));
                    }
                }
            }

        }

        for (Long l : freeMarketHitShelfBatchDTO.getUserCollectionId()) {
            is = newSecondCollectionCommonService.freeMarketHitShelf(l, freeMarketHitShelfBatchDTO.getBuyPrice(), freeMarketHitShelfBatchDTO.getDealPwd());
        }
        Map map = new HashedMap();
        map.put("execute", is);
        return Result.success(map);
    }

    @PostMapping(value = "/getCollectionConsignment", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取藏品的详情信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetUserConsignmentVO> getCollectionConsignment(@RequestBody @Validated IdRequest idRequest) {
        return Result.success(newSecondCollectionCommonService.getCollectionConsignment(idRequest.getId()));
    }

    @PostMapping(value = "/cancelOrderById", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "关闭订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result cancelOrderById(@RequestBody @Validated IdRequest request) {
        newSecondCollectionCommonService.cancelOrderById(request.getId(), UserPool.getUser());
        return Result.success();
    }
    @PostMapping(value = "/batchCancellationOrder", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量关闭订单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result batchCancellationOrder(@RequestBody @Validated SecondBatchCancellationOrderRequest request) {
        request.getIds().forEach(id -> {
            newSecondCollectionCommonService.cancelOrderById(id, UserPool.getUser());
        });
        return Result.success();
    }

    @PostMapping(value = "/myConsignmentList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量下架列表(我的寄售列表)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyConsignmentListVO> myConsignmentList(@Valid @RequestBody PageRequest request) {
        return PageResult.success(newSecondCollectionCommonService.myConsignmentList(UserPool.getUser(), request.getPageNo(), request.getPageSize()));
    }

    @PostMapping(value = "/saveBatchCancellationConsignment", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "批量取消寄售", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result saveBatchCancellationConsignment(@RequestBody @Validated SaveBatchCancellationConsignmentDTO saveBatchCancellationConsignmentDTO) {
        newSecondCollectionCommonService.saveBatchCancellationConsignment(saveBatchCancellationConsignmentDTO);
        return Result.success();
    }

    @PostMapping(value = "/mySalestList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "我的挂单(我的寄售列表)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MySalestListVO> mySalestList(@Valid @RequestBody PageRequest request) {
        return PageResult.success(newSecondCollectionCommonService.mySalestList(UserPool.getUser(), request.getPageNo(), request.getPageSize()));
    }

    @PostMapping(value = "/sellCollectionPriceData", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "自由出售藏品价格数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SellCollectionPriceDataVO> sellCollectionPriceData(@Valid @RequestBody IdRequest idRequest) {
        return Result.success(newSecondCollectionCommonService.sellCollectionPriceData(idRequest.getId()));
    }

}
